<?php

namespace app\admin\controller;

use think\App;
use think\Request;

class Category extends Common
{
    protected $db;

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->db = new \app\common\model\Category();
    }

    /**
     * 栏目管理首页
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $res = $this->db->getAll();
        $this->assign('list_category', $res);
        return $this->fetch();
    }

    /**
     * 添加栏目
     *
     * @param Request $request
     *
     * @return mixed
     */
    public function add(Request $request)
    {
        if ($request->isPost()) {
            $post = $request->param();
            $res = $this->db->add($post);
            if ($res['code']) {
                $this->success($res['message'], 'index');
            } else {
                $this->error($res['message']);
            }
        }
        return $this->fetch();
    }

    /**
     * @param Request $request
     *
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addSon(Request $request)
    {
        if ($request->isPost()) {
            $post = $request->param();
            $results = $this->db->add($post);
            if ($results['code']) {
                $this->success($results['message'], 'index');
            } else {
                $this->error($results['message']);
            }
        }
        $id = $request->param('id');
        $res = $this->db->where('id', $id)->find();
        $this->assign('info', $res);
        return $this->fetch();
    }

    /**
     * 栏目编辑
     *
     * @param Request $request
     *
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(Request $request)
    {
        if ($request->isPost()) {
            $post = $request->param();
            $results = $this->db->edit($post);
            if ($results['code']) {
                $this->success($results['message'], 'index');
            } else {
                $this->error($results['message']);
            }
        }
        $id = $request->param('id');
        $res = $this->db->where('id', $id)->find();
        $p_res = $this->db->where('id', $res['pid'])->find(); //查询父级栏目
        $res['p_name'] = $p_res['name'];
        $this->assign('info', $res);
        return $this->fetch();
    }

    /**
     * @param Request $request
     *
     * @return mixed
     * @throws \Exception
     */
    public function delete(Request $request)
    {
        $id = $request->param('id');
        $res = $this->db->where('id', $id)->delete();
        if (!$res) {
            $this->error('栏目删除失败');
        }
        $this->success('栏目删除成功', 'index');
    }
}